using GroupSix.Domain.Entity.App;
using GroupSix.Domain.Services;
using Microsoft.EntityFrameworkCore;

namespace GroupSix.Infrastructure.Data;

/// <summary>
/// 数据库初始化器
/// </summary>
public static class DbInitializer
{
    /// <summary>
    /// 初始化数据库种子数据
    /// </summary>
    /// <param name="context">数据库上下文</param>
    /// <param name="passwordHasher">密码哈希服务</param>
    public static async Task InitializeAsync(GroupSixDbContext context, IPasswordHasher passwordHasher)
    {
        // 确保数据库已创建
        await context.Database.EnsureCreatedAsync();

        // 初始化角色
        await InitializeRolesAsync(context);

        // 初始化用户
        await InitializeUsersAsync(context, passwordHasher);

        // 初始化菜单
        await InitializeMenusAsync(context);

        // 保存更改
        await context.SaveChangesAsync();
    }

    /// <summary>
    /// 初始化角色数据
    /// </summary>
    private static async Task InitializeRolesAsync(GroupSixDbContext context)
    {
        if (await context.GsRoles.AnyAsync())
            return;

        var roles = new List<GsRole>
        {
            new GsRole("超级管理员", "系统超级管理员，拥有所有权限")
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                Code = "SUPER_ADMIN",
                IsActive = true,
                IsDeleted = false,
                DisplayOrder = 1,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            new GsRole("管理员", "系统管理员，拥有大部分管理权限")
            {
                Id = Guid.Parse("22222222-2222-2222-2222-222222222222"),
                Code = "ADMIN",
                IsActive = true,
                IsDeleted = false,
                DisplayOrder = 2,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            new GsRole("用户", "普通用户，拥有基本功能权限")
            {
                Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
                Code = "USER",
                IsActive = true,
                IsDeleted = false,
                DisplayOrder = 3,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            }
        };

        await context.GsRoles.AddRangeAsync(roles);
    }

    /// <summary>
    /// 初始化用户数据
    /// </summary>
    private static async Task InitializeUsersAsync(GroupSixDbContext context, IPasswordHasher passwordHasher)
    {
        if (await context.GsUsers.AnyAsync())
            return;

        // 超级管理员用户
        var superAdminSalt = passwordHasher.GenerateSalt();
        var superAdminPassword = passwordHasher.HashPassword("superadmin123", superAdminSalt);

        var superAdminUser = new GsUser(
            username: "superadmin",
            passwordHash: superAdminPassword,
            salt: superAdminSalt,
            email: "superadmin@groupsix.com",
            phone: null,
            nickname: "超级管理员",
            avatar: null,
            status: 1)
        {
            Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
            IsActive = true,
            IsDeleted = false,
            DisplayOrder = 1,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow,
            CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
            UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
        };

        // 管理员用户
        var adminSalt = passwordHasher.GenerateSalt();
        var adminPassword = passwordHasher.HashPassword("admin123", adminSalt);

        var adminUser = new GsUser(
            username: "admin",
            passwordHash: adminPassword,
            salt: adminSalt,
            email: "admin@groupsix.com",
            phone: null,
            nickname: "管理员",
            avatar: null,
            status: 1)
        {
            Id = Guid.Parse("44444444-4444-4444-4444-444444444444"),
            IsActive = true,
            IsDeleted = false,
            DisplayOrder = 2,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow,
            CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
            UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
        };

        // 普通用户
        var userSalt = passwordHasher.GenerateSalt();
        var userPassword = passwordHasher.HashPassword("user123", userSalt);

        var normalUser = new GsUser(
            username: "user",
            passwordHash: userPassword,
            salt: userSalt,
            email: "user@groupsix.com",
            phone: null,
            nickname: "普通用户",
            avatar: null,
            status: 1)
        {
            Id = Guid.Parse("55555555-5555-5555-5555-555555555555"),
            IsActive = true,
            IsDeleted = false,
            DisplayOrder = 3,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow,
            CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
            UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
        };

        await context.GsUsers.AddRangeAsync(superAdminUser, adminUser, normalUser);

        // 用户角色关联
        var userRoles = new List<GsUserRole>
        {
            new GsUserRole(superAdminUser.Id, Guid.Parse("11111111-1111-1111-1111-111111111111")) // 超级管理员角色
            {
                Id = Guid.Parse("66666666-6666-6666-6666-666666666666"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            new GsUserRole(adminUser.Id, Guid.Parse("22222222-2222-2222-2222-222222222222")) // 管理员角色
            {
                Id = Guid.Parse("77777777-7777-7777-7777-777777777777"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            new GsUserRole(normalUser.Id, Guid.Parse("33333333-3333-3333-3333-333333333333")) // 普通用户角色
            {
                Id = Guid.Parse("88888888-8888-8888-8888-888888888888"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            }
        };

        await context.GsUserRoles.AddRangeAsync(userRoles);
    }

    /// <summary>
    /// 初始化菜单数据
    /// </summary>
    private static async Task InitializeMenusAsync(GroupSixDbContext context)
    {
        if (await context.GsMenus.AnyAsync())
            return;

        var menus = new List<GsMenu>
        {
            // 系统管理
            new GsMenu("系统管理", "/system", "SettingOutlined", null, 1, "system")
            {
                Id = Guid.Parse("77777777-7777-7777-7777-777777777777"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 用户管理
            new GsMenu("用户管理", "/system/users", "UserOutlined", Guid.Parse("77777777-7777-7777-7777-777777777777"), 1, "system:user")
            {
                Id = Guid.Parse("88888888-8888-8888-8888-888888888888"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 角色管理
            new GsMenu("角色管理", "/system/roles", "TeamOutlined", Guid.Parse("77777777-7777-7777-7777-777777777777"), 2, "system:role")
            {
                Id = Guid.Parse("99999999-9999-9999-9999-999999999999"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 菜单管理
            new GsMenu("菜单管理", "/system/menus", "MenuOutlined", Guid.Parse("77777777-7777-7777-7777-777777777777"), 3, "system:menu")
            {
                Id = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 日志管理
            new GsMenu("日志管理", "/system/logs", "FileTextOutlined", Guid.Parse("77777777-7777-7777-7777-777777777777"), 4, "operationlog")
            {
                Id = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            }
        };

        await context.GsMenus.AddRangeAsync(menus);

        // 角色菜单关联（超级管理员拥有所有菜单权限）
        var roleMenus = new List<GsRoleMenu>
        {
            // 超级管理员 - 系统管理
            new GsRoleMenu(Guid.Parse("11111111-1111-1111-1111-111111111111"), Guid.Parse("77777777-7777-7777-7777-777777777777"))
            {
                Id = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 超级管理员 - 用户管理
            new GsRoleMenu(Guid.Parse("11111111-1111-1111-1111-111111111111"), Guid.Parse("88888888-8888-8888-8888-888888888888"))
            {
                Id = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 超级管理员 - 角色管理
            new GsRoleMenu(Guid.Parse("11111111-1111-1111-1111-111111111111"), Guid.Parse("99999999-9999-9999-9999-999999999999"))
            {
                Id = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 超级管理员 - 菜单管理
            new GsRoleMenu(Guid.Parse("11111111-1111-1111-1111-111111111111"), Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"))
            {
                Id = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 超级管理员 - 日志管理
            new GsRoleMenu(Guid.Parse("11111111-1111-1111-1111-111111111111"), Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"))
            {
                Id = Guid.Parse("11111111-1111-1111-1111-111111111112"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 管理员 - 系统管理
            new GsRoleMenu(Guid.Parse("22222222-2222-2222-2222-222222222222"), Guid.Parse("77777777-7777-7777-7777-777777777777"))
            {
                Id = Guid.Parse("22222222-2222-2222-2222-222222222223"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 管理员 - 用户管理
            new GsRoleMenu(Guid.Parse("22222222-2222-2222-2222-222222222222"), Guid.Parse("88888888-8888-8888-8888-888888888888"))
            {
                Id = Guid.Parse("22222222-2222-2222-2222-222222222224"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            },
            // 管理员 - 日志管理
            new GsRoleMenu(Guid.Parse("22222222-2222-2222-2222-222222222222"), Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"))
            {
                Id = Guid.Parse("22222222-2222-2222-2222-222222222225"),
                IsActive = true,
                IsDeleted = false,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                UpdatedBy = Guid.Parse("00000000-0000-0000-0000-000000000000")
            }
        };

        await context.GsRoleMenus.AddRangeAsync(roleMenus);
    }
} 